package com.lungle.warmdiary.app.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lungle.warmdiary.R;

/**
 * Created by God on 2016/8/16.
 */
public class InputItemView extends LinearLayout implements TextWatcher {
    private TextView mErrorTip;
    private EditText mEtInput;
    private ImageView mIvClear;

    public InputItemView(Context context) {
        super(context);
        initView(context);
    }


    public InputItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.InputItemView);
        initWidget(ta);

    }

    public InputItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.InputItemView);
        initWidget(ta);

    }

    private void initWidget(TypedArray ta) {

        mEtInput = (EditText) findViewById(R.id.et_input);


        mIvClear = (ImageView) findViewById(R.id.iv_clear);
        mIvClear.setOnClickListener(v -> {
            if (mEtInput != null) {
                mEtInput.getText().clear();
                mEtInput.requestFocus();
            }
        });
        mErrorTip = (TextView) findViewById(R.id.tv_error_tip);
        mEtInput.addTextChangedListener(this);
        String text = ta.getString(R.styleable.InputItemView_text);
        String hint = ta.getString(R.styleable.InputItemView_hint);

        float textSize = ta.getDimension(R.styleable.InputItemView_textSize, 14);
        mEtInput.setTextSize(textSize);
        mErrorTip.setTextSize(textSize);

        mEtInput.setSingleLine(true);
        boolean isPwd = ta.getBoolean(R.styleable.InputItemView_isPassword, false);

        if (isPwd) {
            mEtInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
//            mEtInput.setKeyListener(DigitsKeyListener.getInstance("0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"));
//            mEtInput.setMaxEms(12);
        }

        boolean isEmail = ta.getBoolean(R.styleable.InputItemView_isEmail, false);
        if (isEmail) {
            mEtInput.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);

        }

        boolean isNumber = ta.getBoolean(R.styleable.InputItemView_isNumber, false);
        if (isNumber) {
            mEtInput.setInputType(InputType.TYPE_CLASS_NUMBER);
        }

        boolean isPhone = ta.getBoolean(R.styleable.InputItemView_isPhone, false);
        if (isPhone) {
            mEtInput.setInputType(InputType.TYPE_CLASS_PHONE);

        }

        boolean isEnabled = ta.getBoolean(R.styleable.InputItemView_enabled, true);
        if (!isEnabled) {
            mEtInput.setEnabled(false);
            mEtInput.removeTextChangedListener(this);
            mIvClear.setVisibility(INVISIBLE);
        } else {
            mEtInput.setEnabled(true);
            mEtInput.addTextChangedListener(this);
        }

        mEtInput.setHint(hint);
        mEtInput.setText(text);


        ta.recycle();

    }


    public void setEnable(boolean enable) {
        if (enable) {
            mEtInput.setEnabled(true);
            mEtInput.addTextChangedListener(this);
            mIvClear.setVisibility(VISIBLE);
        } else {
            mEtInput.setEnabled(false);
            mEtInput.removeTextChangedListener(this);
            mIvClear.setVisibility(INVISIBLE);
        }
    }

    private void initView(Context context) {
        View.inflate(context, R.layout.view_input, this);


    }

    public void setText(String text) {
        mErrorTip.setVisibility(INVISIBLE);
        mEtInput.setText(text);
    }

    public void setErrorTip(String error) {
        if (mIvClear.getVisibility() != GONE) mIvClear.setVisibility(GONE);
        mErrorTip.setVisibility(VISIBLE);
        mErrorTip.setText(error);
        mEtInput.requestFocus();
    }

    public String getInputText() {
        return mEtInput.getText().toString();
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (mErrorTip.getVisibility() != GONE) mErrorTip.setVisibility(GONE);
        mIvClear.setVisibility(TextUtils.isEmpty(s) ? View.INVISIBLE : View.VISIBLE);
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
